import type { Request, Response } from 'express';

// 组织树形数据
const mockOrgTreeData = [
  {
    departmentId: '1',
    departmentName: '总公司',
    parentId: null,
    memberCount: 150,
    children: [
      {
        departmentId: '1-1',
        departmentName: '研发中心',
        parentId: '1',
        memberCount: 60,
        children: [
          {
            departmentId: '1-1-1',
            departmentName: '前端开发部',
            parentId: '1-1',
            memberCount: 25,
            children: [
              {
                departmentId: '1-1-1-1',
                departmentName: 'Web前端组',
                parentId: '1-1-1',
                memberCount: 15,
                children: [],
              },
              {
                departmentId: '1-1-1-2',
                departmentName: '移动端开发组',
                parentId: '1-1-1',
                memberCount: 10,
                children: [],
              },
            ],
          },
          {
            departmentId: '1-1-2',
            departmentName: '后端开发部',
            parentId: '1-1',
            memberCount: 20,
            children: [
              {
                departmentId: '1-1-2-1',
                departmentName: 'Java开发组',
                parentId: '1-1-2',
                memberCount: 12,
                children: [],
              },
              {
                departmentId: '1-1-2-2',
                departmentName: 'Node.js开发组',
                parentId: '1-1-2',
                memberCount: 8,
                children: [],
              },
            ],
          },
          {
            departmentId: '1-1-3',
            departmentName: '测试部',
            parentId: '1-1',
            memberCount: 15,
            children: [
              {
                departmentId: '1-1-3-1',
                departmentName: '自动化测试组',
                parentId: '1-1-3',
                memberCount: 8,
                children: [],
              },
              {
                departmentId: '1-1-3-2',
                departmentName: '功能测试组',
                parentId: '1-1-3',
                memberCount: 7,
                children: [],
              },
            ],
          },
        ],
      },
      {
        departmentId: '1-2',
        departmentName: '产品中心',
        parentId: '1',
        memberCount: 30,
        children: [
          {
            departmentId: '1-2-1',
            departmentName: '产品设计部',
            parentId: '1-2',
            memberCount: 15,
            children: [
              {
                departmentId: '1-2-1-1',
                departmentName: 'UI设计组',
                parentId: '1-2-1',
                memberCount: 8,
                children: [],
              },
              {
                departmentId: '1-2-1-2',
                departmentName: 'UX设计组',
                parentId: '1-2-1',
                memberCount: 7,
                children: [],
              },
            ],
          },
          {
            departmentId: '1-2-2',
            departmentName: '产品运营部',
            parentId: '1-2',
            memberCount: 15,
            children: [
              {
                departmentId: '1-2-2-1',
                departmentName: '用户运营组',
                parentId: '1-2-2',
                memberCount: 8,
                children: [],
              },
              {
                departmentId: '1-2-2-2',
                departmentName: '内容运营组',
                parentId: '1-2-2',
                memberCount: 7,
                children: [],
              },
            ],
          },
        ],
      },
      {
        departmentId: '1-3',
        departmentName: '市场部',
        parentId: '1',
        memberCount: 25,
        children: [
          {
            departmentId: '1-3-1',
            departmentName: '品牌推广组',
            parentId: '1-3',
            memberCount: 12,
            children: [],
          },
          {
            departmentId: '1-3-2',
            departmentName: '商务拓展组',
            parentId: '1-3',
            memberCount: 13,
            children: [],
          },
        ],
      },
      {
        departmentId: '1-4',
        departmentName: '人力资源部',
        parentId: '1',
        memberCount: 18,
        children: [
          {
            departmentId: '1-4-1',
            departmentName: '招聘组',
            parentId: '1-4',
            memberCount: 8,
            children: [],
          },
          {
            departmentId: '1-4-2',
            departmentName: '培训发展组',
            parentId: '1-4',
            memberCount: 6,
            children: [],
          },
          {
            departmentId: '1-4-3',
            departmentName: '薪酬福利组',
            parentId: '1-4',
            memberCount: 4,
            children: [],
          },
        ],
      },
      {
        departmentId: '1-5',
        departmentName: '财务部',
        parentId: '1',
        memberCount: 12,
        children: [
          {
            departmentId: '1-5-1',
            departmentName: '会计组',
            parentId: '1-5',
            memberCount: 7,
            children: [],
          },
          {
            departmentId: '1-5-2',
            departmentName: '出纳组',
            parentId: '1-5',
            memberCount: 5,
            children: [],
          },
        ],
      },
      {
        departmentId: '1-6',
        departmentName: '行政部',
        parentId: '1',
        memberCount: 5,
        children: [],
      },
    ],
  },
];

export default {
  // 获取组织树形列表
  'POST /api/anyics-front/orgTreeList': (req: Request, res: Response) => {
    // 模拟延迟
    setTimeout(() => {
      res.json({
        code: 200,
        data: mockOrgTreeData,
        message: '获取成功',
        success: true,
      });
    }, 300);
  },

  // 新增组织
  'POST /api/org/create': (req: Request, res: Response) => {
    const { departmentName, parentId } = req.body;

    setTimeout(() => {
      res.json({
        code: 200,
        data: {
          departmentId: Math.random().toString(36).substr(2, 9),
          departmentName,
          parentId,
          memberCount: 0,
        },
        message: '新增成功',
        success: true,
      });
    }, 500);
  },

  // 编辑组织
  'POST /api/org/edit': (req: Request, res: Response) => {
    const { departmentId, departmentName } = req.body;

    setTimeout(() => {
      res.json({
        code: 200,
        data: {
          departmentId,
          departmentName,
        },
        message: '编辑成功',
        success: true,
      });
    }, 500);
  },

  // 删除组织
  'POST /api/org/delete': (req: Request, res: Response) => {
    const { departmentId } = req.body;

    setTimeout(() => {
      res.json({
        code: 200,
        data: { departmentId },
        message: '删除成功',
        success: true,
      });
    }, 500);
  },
};
